//
//  InitViewModel.swift
//  IdleFish
//
//  Created by Ben on 2021/11/28.
//

import SwiftUI
import Combine

// UserManager相关的网络请求协议

protocol InitRequest {
    
    func requestInit(token: String) async throws -> NetworkBaseModel<InitModel>
}
extension InitRequest {
    
    func requestInit(token: String) async throws -> NetworkBaseModel<InitModel> {
        let result = try await NetworkTool.awaitRequest(request: API.`init`(token: token), responseType: InitModel.self)
        
        return result
    }
}

@MainActor
class InitViewModel: ObservableObject, InitRequest, BenHUDAble {
    
    @Published var initModel: InitModel?
    
    var userInfo: UserModel? {
        return initModel?.user_info
    }
    
    func requestInit(token: String) async {
        
        do {
            initModel = try await requestInit(token: token).data
        } catch {
            
            if let e = error as? APIError {
                switch e {
                case .custom(let msg):
                    self.show(message: msg, systemImageName: "xmark.seal.fill")
                }
            }
        }
       
        
    }
    
    func userInfoLovedValueInUserData() -> [String] {
        return ["动漫"]
    }
    func userInfoDefaultValueInUserData(userDataAction: UserDataAction) -> String {
        
        var value: String?
        switch userDataAction {
        case .handle:
            value = userInfo?.avatar
        case .account:
            value = userInfo?.account
        case .nickName:
            value = userInfo?.nick_name
        case .gender:
            value = userInfo?.genderValue
        case .birthday:
            value = Date.str(timeInt: Int(userInfo?.birthday ?? "0")!, format: "yyyy-MM-dd")
        case .address:
            value = userInfo?.address
        case .introduceBriefly:
            value = userInfo?.introduce_briefly

        case .industry:
            value = userInfo?.industry

        case .position:
            value = userInfo?.position

        case .school:
            value = userInfo?.school

        default:
            ()
        }
        return value ?? ""
    }

}
